# from django.contrib.admin import site, ModelAdmin
from crm import models
from king_admin.service.stark import site, ModelStark
from django.utils.safestring import mark_safe
from django.urls import re_path
from django.shortcuts import redirect
# Register your models here.

class CustomerAdmin(ModelStark):

    def display_gender(self, obj=None, header=False):  # get_gender_display获得数据库中choice字段的value
        if header:
            return '性别'
        return obj.get_gender_display()


    def display_tags(self, obj=None, header=False):
        if header:
            return '标签'
        temp = []
        for tag in obj.tags.all():
            s = "<a href='/king_admin/crm/customer/cancel_tag/%s/%s' style='border:1px solid #369;padding:3px 6px'><span>%s</span></a>&nbsp"%(obj.pk, tag.pk, tag)
            temp.append(s)
        return mark_safe("".join(temp))

    def cancel_tag(self, request, customer_id, tag_id):
        obj = models.Customer.objects.filter(pk=customer_id).first()
        obj.tags.remove(tag_id)
        return redirect('/king_admin/crm/customer/')

    def extra_url(self):
        temp = []
        temp.append(re_path(r'cancel_tag/(\d+)/(\d+)', self.cancel_tag))
        return temp

    list_display = ('name', display_gender, 'qq', 'consultant', display_tags, 'content', 'date')
    list_filter = ('consultant', 'tags')
    search_fields = ('qq', 'name')
    # raw_id_fields = ('consult_course',)
    # filter_horizontal = ('tags',)
    # list_editable = ('status',)

class UserprofileAdmin(ModelStark):
    list_display = ('name', 'roles')

class ClassListAdmin(ModelStark):
    list_display = ('__str__', 'teachers',)


site.register(models.Customer, CustomerAdmin)
site.register(models.CustomerFollowUp)
site.register(models.Enrollment)
site.register(models.Course)
site.register(models.ClassList, ClassListAdmin)
site.register(models.CourseRecord)
site.register(models.Branch)
site.register(models.Role)
site.register(models.Payment)
site.register(models.StudyRecord)
site.register(models.UserProfile, UserprofileAdmin)
site.register(models.Tag)
site.register(models.Menu)









